import { LOGIN_TYPE_OAUTH_NO_REGISTER } from '@/constants';
import { MMessage } from '@/libs';
import router from '@/router';
import { useUserStore } from '@/store';

export default function useOAuth() {
  const userStore = useUserStore();

  const oauthLogin = async (oauthType: 'QQ' | 'WX', oauthData: any) => {
    const code = await userStore.login({ loginType: oauthType, ...oauthData });

    // 注册
    if (code && code === LOGIN_TYPE_OAUTH_NO_REGISTER) {
      MMessage({
        content: `欢迎您 ${oauthData.nickname}, 请创建您的账号`,
        duration: 6000,
        type: 'success'
      });

      router.push({
        path: '/register',
        query: {
          reqType: oauthType,
          ...oauthData
        }
      });
    }

    router.push({ path: '/' });
  };

  return { oauthLogin };
}
